package me.com.sharedprefernces2;


import java.util.Locale;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {
    private Button btnOpenPreferences;
    private Locale overrideLocale;
        
    private int counter = 0;
    
    boolean checkboxPref;
    EditTextPreference editText;
    String listPref;
    public static String locale_prefPref = "en";
    
    private SampleReceiver myReceiver;
    public static final String NEW_MSG = "me.com.sharedprefernces2.NEW_MSG";
    
    private SharedPreferences preferences;
    public final static String MY_PREFS = "myPrefs";
    
    private OnClickListener btnOpenPreferencesListener = new OnClickListener() {
        @Override
        public void onClick(View arg0) {
            Intent intent = new Intent(MainActivity.this, MyPreferences.class);
            startActivity(intent);    

            //send broadcast to widget
            //Intent intentWidget = new Intent(MyAppWidget.UPDATE_ONDEMAND);
            //intentWidget.putExtra("WidgetText", "Service counter: " + (++counter));
            //sendBroadcast(intentWidget);             
        }
    };
     
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d("OLO", "___________onCreate___________");
        super.onCreate(savedInstanceState);       
        
        //Wczytaj paczke z preferencjami
        preferences = getSharedPreferences(MY_PREFS, Activity.MODE_PRIVATE);
        
        //String textFromPreferences = preferences.getString("edittext", "");
        //checkboxPref = preferences.getBoolean("checkbox", false);
        //listPref = preferences.getString("list", "");
        locale_prefPref = preferences.getString("locale_pref", "");
                
        allowLocaleOverride();
                Locale locale = Locale.getDefault();
                Log.d("OLO", "Locale language:"+locale.getLanguage()+" Variant:"+locale.getDisplayName());
        
        
                 setContentView(R.layout.main);
             btnOpenPreferences = (Button)findViewById(R.id.btnOpenPreferences);
             btnOpenPreferences.setOnClickListener(btnOpenPreferencesListener);
             
             //BroadcastReceiver
             myReceiver = new SampleReceiver();
             IntentFilter filter = new IntentFilter(NEW_MSG);
             registerReceiver(myReceiver, filter);
    }    

        /** Allow user interface locale override by changing Settings
         */
        private void allowLocaleOverride() {
                // Has the user selected a custom locale?
                Configuration config = getBaseContext().getResources().getConfiguration();
                String lang = preferences.getString("locale_pref", "");
                Log.d("OLO", "allowLocaleOverride - Preference language:"+lang);
                overrideLocale = new Locale(lang);
                Locale.setDefault(overrideLocale);
                config.locale = overrideLocale;
                getBaseContext().getResources().updateConfiguration(config,     getBaseContext().getResources().getDisplayMetrics());           
        }
        
        /** Update screen after locale preferences change
         */
        private void localeChange(String localePrefs) {
                Configuration config = getBaseContext().getResources().getConfiguration();
                
                Log.d("OLO", "localeChange - Preference language:"+localePrefs);
                overrideLocale = new Locale(localePrefs);
                Locale.setDefault(overrideLocale);
                config.locale = overrideLocale;
                getBaseContext().getResources().updateConfiguration(config,     getBaseContext().getResources().getDisplayMetrics());
                
                //zmiana jezyka
                finish();
                Intent myIntent = new Intent(this, MainActivity.class); 
                startActivity(myIntent);
        }
        
        /*@Override
        protected void onResume() {
            //if (!(PreferenceManager.getDefaultSharedPreferences(
                  //  getApplicationContext()).getString("listLanguage", "en")
                //    .equals(langPreference))) {
              //  refresh();
            //}
                String localeTemp = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()).getString("locale_pref", "");
                Log.d("OLO", "**************** onResume **************** " + localeTemp+"      locale_prefPref: "+locale_prefPref);
                if (!localeTemp.equals(locale_prefPref)) {
                        Log.d("OLO", "**************** onResume **************** zmiana jezyka");
                        Configuration config = getBaseContext().getResources().getConfiguration();
                        //locale_prefPref = localeTemp;
                        overrideLocale = new Locale(localeTemp);
                        Locale.setDefault(overrideLocale);
                        config.locale = overrideLocale;
                        getBaseContext().getResources().updateConfiguration(config,     getBaseContext().getResources().getDisplayMetrics());
                        
                        refresh();
                }
            super.onResume();
        }

        private void refresh() {
                Log.d("OLO", "**************** refresh() **************** ");
                finish();
            Intent myIntent = new Intent(MainActivity.this, MainActivity.class);
            startActivity(myIntent);
        }*/


        /* receive message form preferences */
        private class SampleReceiver extends BroadcastReceiver {           
                @Override
                public void onReceive(Context context, Intent intent) {
                        // TODO Auto-generated method stub
                        String localeTemp = intent.getStringExtra("ReceiverData");
                Log.d("OLO", "**************** SampleReceiver **************** " + localeTemp +" overrideLocale: "+overrideLocale);
                
                if (!localeTemp.equals(locale_prefPref)) {
                        //Log.d("OLO", "**************** SampleReceiver **************** zmiana jezyka");
                        localeChange(localeTemp);                       
                }
                }
        }
        
}
